import 'package:boss/ui/page/printer/constant/printer_constant.dart';
import 'package:json_annotation/json_annotation.dart';

part 'cloud_printer.g.dart';

@JsonSerializable()
class CloudPrinter extends Object {
  @JsonKey(name: 'buzzingType')
  int buzzingType;

  @JsonKey(name: 'cloudKey')
  String cloudKey;

  @JsonKey(name: 'cloudSn')
  String cloudSn;

  @JsonKey(name: 'connectModel')
  int connectModel;

  @JsonKey(name: 'cutType', defaultValue: PrinterConstant.CUT_ORDER)
  int cutType;

  @JsonKey(name: 'deviceCode')
  String deviceCode;

  @JsonKey(name: 'deviceId')
  int deviceId;

  @JsonKey(name: 'deviceUuid')
  String deviceUuid;

  @JsonKey(name: 'id')
  int id;

  @JsonKey(name: 'mergeType')
  int mergeType;

  @JsonKey(name: 'orderByType', defaultValue: PrinterConstant.ORDER_BY_MENU)
  int orderByType;

  @JsonKey(name: 'paperWidth', defaultValue: 57)
  int paperWidth;

  @JsonKey(name: 'printerBrand', defaultValue: '飞鹅')
  String printerBrand;

  @JsonKey(name: 'printerDevice')
  String printerDevice;

  @JsonKey(name: 'printerGuid')
  String printerGuid;

  @JsonKey(name: 'printerIp')
  String printerIp;

  @JsonKey(name: 'printerName')
  String printerName;

  @JsonKey(name: 'printerPlanAreaList')
  List<PrinterPlanAreaList> printerPlanAreaList;

  @JsonKey(name: 'printerPlanBillList')
  List<PrinterPlanBillList> printerPlanBillList;

  @JsonKey(name: 'printerPlanFoodList')
  List<PrinterPlanFoodList> printerPlanFoodList;

  @JsonKey(name: 'status')
  int status;

  @JsonKey(name: 'printerPlanId')
  int printerPlanId;

  @JsonKey(name: 'printerDeviceId')
  int printerDeviceId;

  @JsonKey(name: 'type', defaultValue: PrinterConstant.FRONT_PRINTER)
  int type;

  String get printerPlanFoodListText {
    if (printerPlanFoodList == null || printerPlanFoodList.length == 0) {
      return null;
    }

    var text = '';
    printerPlanFoodList.forEach((item) {
      text += '${item.commodityName}';

      if (printerPlanFoodList.last != item) text += ',';
    });
    return text;
  }

  String get printerPlanAreaListText {
    if (printerPlanAreaList == null || printerPlanAreaList.length == 0) {
      return null;
    }

    var text = '';
    printerPlanAreaList.forEach((item) {
      text += '${item.areaName}';

      if (printerPlanAreaList.last != item) text += ',';
    });
    return text;
  }

  String get printerPlanBillListText {
    if (printerPlanBillList == null) {
      return null;
    }

    var filters = printerPlanBillList.where((item) => item.count > 0).toList();
    if (filters.length == printerPlanBillList.length) {
      return '全部';
    }

    var text = '';

    filters.forEach((item) {
      text += item.billName;

      if (item != filters.last) text += ',';
    });

    return text;
  }

  CloudPrinter(
    this.buzzingType,
    this.cloudKey,
    this.cloudSn,
    this.connectModel,
    this.cutType,
    this.deviceCode,
    this.deviceId,
    this.deviceUuid,
    this.id,
    this.mergeType,
    this.orderByType,
    this.paperWidth,
    this.printerBrand,
    this.printerDevice,
    this.printerGuid,
    this.printerIp,
    this.printerName,
    this.printerPlanAreaList,
    this.printerPlanBillList,
    this.printerPlanFoodList,
    this.status,
    this.type,
  );

  factory CloudPrinter.fromJson(Map<String, dynamic> srcJson) => _$CloudPrinterFromJson(srcJson);

  Map<String, dynamic> toJson() => _$CloudPrinterToJson(this);
}

@JsonSerializable()
class PrinterPlanAreaList extends Object {
  @JsonKey(name: 'areaId')
  int areaId;

  @JsonKey(name: 'areaName')
  String areaName;

  PrinterPlanAreaList(
    this.areaId,
    this.areaName,
  );

  factory PrinterPlanAreaList.fromJson(Map<String, dynamic> srcJson) => _$PrinterPlanAreaListFromJson(srcJson);

  Map<String, dynamic> toJson() => _$PrinterPlanAreaListToJson(this);
}

@JsonSerializable()
class PrinterPlanBillList extends Object {
  @JsonKey(name: 'billName')
  String billName;

  @JsonKey(name: 'count', defaultValue: 1)
  int count;

  @JsonKey(name: 'printerBillId')
  int printerBillId;

  @JsonKey(name: 'type')
  int type;

  PrinterPlanBillList(
    this.billName,
    this.count,
    this.printerBillId,
  );

  factory PrinterPlanBillList.fromJson(Map<String, dynamic> srcJson) => _$PrinterPlanBillListFromJson(srcJson);

  Map<String, dynamic> toJson() => _$PrinterPlanBillListToJson(this);
}

@JsonSerializable()
class PrinterPlanFoodList extends Object {
  @JsonKey(name: 'commodityId')
  int commodityId;

  @JsonKey(name: 'commodityName')
  String commodityName;

  @JsonKey(name: 'menuId')
  int menuId;

  @JsonKey(name: 'menuName')
  String menuName;

  PrinterPlanFoodList(
    this.commodityId,
    this.commodityName,
    this.menuId,
    this.menuName,
  );

  factory PrinterPlanFoodList.fromJson(Map<String, dynamic> srcJson) => _$PrinterPlanFoodListFromJson(srcJson);

  Map<String, dynamic> toJson() => _$PrinterPlanFoodListToJson(this);
}
